package cn.mumzone.pos.presenter;

import java.util.HashMap;
import java.util.List;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.net.response.PageListResponse;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.event.MoreViewChangeEvent;
import cn.mumzone.pos.event.StockSearchResultEvent;
import cn.mumzone.pos.model.GoodsTreeEntity;
import cn.mumzone.pos.model.RepoListGoodsEntity;
import cn.mumzone.pos.model.RepositoryEntity;
import cn.mumzone.pos.model.source.GoodsRepository;
import cn.mumzone.pos.model.source.StockRepository;
import cn.mumzone.pos.presenter.contract.StockCheckContract;
import cn.mumzone.pos.presenter.contract.StockCheckSearchContract;
import rx.Subscription;
import rx.functions.Action1;

/**
 * StockCheckPresenter
 * 盘点页面的 Presenter，
 * 主要工作有：1. 查询仓库；2. 扫码查询商品并通过弹窗展示；
 * 3. 生成盘点单 4. 使用 RxBus 接收手动选择后回传的列表数据
 */
public class StockCheckPresenter extends AbsPresenter<StockCheckContract.View, StockRepository> implements StockCheckContract.Presenter {


    private int mPageNum = 1;

    GoodsRepository goodsRepository;

    @Override
    protected void initOtherRepository() {
        super.initOtherRepository();
        goodsRepository = new GoodsRepository();
    }

    public StockCheckPresenter() {
        Subscription subscribe = RxBus.getDefault().toObservable(StockSearchResultEvent.class)
                .subscribe(new Action1<StockSearchResultEvent>() {
                    @Override
                    public void call(StockSearchResultEvent stockSearchResultEvent) {
                        mView.onGoodSelected(stockSearchResultEvent.getSelectedItems());
                    }
                });

        addSubscrebe(subscribe);

        Subscription moreViewChangeListener = RxBus.getDefault()
                .toObservable(MoreViewChangeEvent.class)
                .subscribe(new Action1<MoreViewChangeEvent>() {
                    @Override
                    public void call(MoreViewChangeEvent moreViewChangeEvent) {
                        mView.onBackPressed(moreViewChangeEvent.getType());
                    }
                });
        addSubscrebe(moreViewChangeListener);
    }

    @Override
    public void loadRepositoryList() {

        ApiCallBack<PageListResponse<RepositoryEntity>> callBack = new ApiCallBack<PageListResponse<RepositoryEntity>>() {
            @Override
            public void onSuccess(PageListResponse<RepositoryEntity> response) {
                mView.showRepositoryList(response.getList());
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };

        mRepo.getRepositoryListPage(callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void loadRepoGoodsById(String repositoryId, String keyword, String type) {
        ApiCallBack<PageListResponse<RepoListGoodsEntity>> callBack = new ApiCallBack<PageListResponse<RepoListGoodsEntity>>() {
            @Override
            public void onSuccess(PageListResponse<RepoListGoodsEntity> response) {
                if (!ListUtil.isListEmpty(response.getList())) {
                    mView.showScanGoodsDialog(response.getList());
                } else {
                    mView.showError("仓库内不存在该商品");
                }
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };

        mRepo.findRepoGoodsByRepoId(repositoryId, "", keyword,
                "1", String.valueOf(AppConstants.PAGE_SIZE), type, callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void generateCheckBill(String warehouseId, String warehouseName, String inventoryType, String goodsJson) {
        ApiCallBack<String> callBack = new ApiCallBack<String>() {
            @Override
            public void onSuccess(String response) {
                mView.onCheckBillGenerated();
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("creater", AppConstants.TEMP_SELLER_ID);
        hashMap.put("shopName", AppConstants.TEMP_SHOP_NAME);
        hashMap.put("inventoryStatus", "0");

        hashMap.put("warehouseId", warehouseId);
        hashMap.put("warehouseName", warehouseName);
        hashMap.put("inventoryType", inventoryType);
        hashMap.put("inventoryPattern", "1");//0盲盘 1明盘
        hashMap.put("goodsInventoryDetails", goodsJson);

        mRepo.generateCheckStockBill(callBack, hashMap);
        addSubscrebe(callBack);
    }


}
